﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 组件服务
    /// </summary>
    public class MaterialSubProductService : IMaterialSubProductService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Material_SubProduct model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(List<string> ids)
        {
            try
            {
                dal.BeginTran();

                bool result = dal.Updateable<T_Material_SubProduct>(new
                {
                    DataStatus = DataStatusEnum.Deleted,
                    ModifyTime = DateTime.Now,
                }).Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;

                if (result == false)
                {
                    dal.RollbackTran();
                    return false;
                }

                result = dal.Updateable<T_Material_SubProductDtl>(new
                {
                    DataStatus = DataStatusEnum.Deleted,
                }).Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;

                if (result == false)
                {
                    dal.RollbackTran();
                    return false;
                }

                dal.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                dal.RollbackTran();
                throw ex;
            }
        }

        public T_Material_SubProduct GetById(string id)
        {
            return dal.Get<T_Material_SubProduct>(id);
        }

        public List<T_Material_SubProduct> GetList(string companyID)
        {
            Expression<Func<T_Material_SubProduct, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(companyID))
            {
                whereExp = whereExp.Compose((a) => a.CompanyID == companyID, Expression.AndAlso);
            }
            return dal.Queryable<T_Material_SubProduct>().Where(whereExp).ToList();
        }

        public PageData<T_Material_SubProduct> Query(string companyID, string subProductCode, string subProductName, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Material_SubProduct, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(companyID))
            {
                whereExp = whereExp.Compose((a) => a.CompanyID == companyID, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(subProductCode))
            {
                whereExp = whereExp.Compose((a) => a.SubProductCode.StartsWith(subProductCode), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(subProductName))
            {
                whereExp = whereExp.Compose((a) => a.SubProductName.StartsWith(subProductName), Expression.AndAlso);
            }
            List<T_Material_SubProduct> list = dal.Queryable<T_Material_SubProduct>().Where(whereExp).OrderBy(a => a.CreateTime, SqlSugar.OrderByType.Desc).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Material_SubProduct> { Data = list, Page = page };
        }

        public bool Update(T_Material_SubProduct model)
        {
            return dal.Updateable<T_Material_SubProduct>(p => new T_Material_SubProduct()
            {
                SubProductCode = model.SubProductCode,
                SubProductName = model.SubProductName,
                ModifyUserId = model.ModifyUserId,
                ModifyUserName = model.ModifyUserName
            }).Where(p => p.ID == model.ID).ExecuteCommand() > 0;
        }
    }
}
